<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['boss:managementOne:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['boss:managementOne:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['boss:managementOne:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['boss:managementOne:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="managementOneList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="发票ID" align="center" prop="invoiceId" />
      <el-table-column label="关联用户ID" align="center" prop="userId" />
      <el-table-column label="发票状态" align="center" prop="invoiceStatus">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_invoice_status" :value="scope.row.invoiceStatus"/>
        </template>
      </el-table-column>
      <el-table-column label="驳回原因" align="center" prop="rejectReason" />
      <el-table-column label="发票抬头" align="center" prop="invoiceTitle" />
      <el-table-column label="纳税人识别号" align="center" prop="taxIdentifier" />
      <el-table-column label="发票类型" align="center" prop="invoiceType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_invoice_type" :value="scope.row.invoiceType"/>
        </template>
      </el-table-column>
      <el-table-column label="发票号码" align="center" prop="invoiceNumber" />
      <el-table-column label="发票主体" align="center" prop="invoiceSubject">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_invoice_subject" :value="scope.row.invoiceSubject"/>
        </template>
      </el-table-column>
      <el-table-column label="发票内容" align="center" prop="invoiceContent" />
      <el-table-column label="发票金额" align="center" prop="invoiceAmount" />
      <el-table-column label="注册地址" align="center" prop="registeredAddress" />
      <el-table-column label="注册电话" align="center" prop="registeredPhone" />
      <el-table-column label="开户银行" align="center" prop="bankName" />
      <el-table-column label="银行账号" align="center" prop="bankAccount" />
      <el-table-column label="收件人姓名" align="center" prop="recipientName" />
      <el-table-column label="收件人电话" align="center" prop="recipientPhone" />
      <el-table-column label="收件地址" align="center" prop="recipientAddress" />
      <el-table-column label="行程单接收邮箱" align="center" prop="recipientEmail" />
      <el-table-column label="快递公司" align="center" prop="expressCompany">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.t_experss_company" :value="scope.row.expressCompany"/>
        </template>
      </el-table-column>
      <el-table-column label="快递单号" align="center" prop="trackingNumber" />
      <el-table-column label="行程单接收邮箱" align="center" prop="shippingTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.shippingTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请日期" align="center" prop="applicationDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.applicationDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="行程开始日期" align="center" prop="travelStartDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.travelStartDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="行程结束日期" align="center" prop="travelEndDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.travelEndDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="行程人手机号" align="center" prop="travelerPhone" />
      <el-table-column label="行程数量" align="center" prop="travelCount" />
      <el-table-column label="行程合计金额" align="center" prop="travelAmount" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['boss:managementOne:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['boss:managementOne:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改发票详情对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="关联用户ID" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入关联用户ID" />
        </el-form-item>
        <el-form-item label="发票状态" prop="invoiceStatus">
          <el-select v-model="form.invoiceStatus" placeholder="请选择发票状态">
            <el-option
              v-for="dict in dict.type.t_invoice_status"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="驳回原因" prop="rejectReason">
          <el-input v-model="form.rejectReason" placeholder="请输入驳回原因" />
        </el-form-item>
        <el-form-item label="发票抬头" prop="invoiceTitle">
          <el-input v-model="form.invoiceTitle" placeholder="请输入发票抬头" />
        </el-form-item>
        <el-form-item label="纳税人识别号" prop="taxIdentifier">
          <el-input v-model="form.taxIdentifier" placeholder="请输入纳税人识别号" />
        </el-form-item>
        <el-form-item label="发票类型" prop="invoiceType">
          <el-select v-model="form.invoiceType" placeholder="请选择发票类型">
            <el-option
              v-for="dict in dict.type.t_invoice_type"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="发票号码" prop="invoiceNumber">
          <el-input v-model="form.invoiceNumber" placeholder="请输入发票号码" />
        </el-form-item>
        <el-form-item label="发票主体" prop="invoiceSubject">
          <el-input v-model="form.invoiceSubject" placeholder="请输入发票主体" />
        </el-form-item>
        <el-form-item label="发票内容">
          <editor v-model="form.invoiceContent" :min-height="192"/>
        </el-form-item>
        <el-form-item label="发票金额" prop="invoiceAmount">
          <el-input v-model="form.invoiceAmount" placeholder="请输入发票金额" />
        </el-form-item>
        <el-form-item label="注册地址" prop="registeredAddress">
          <el-input v-model="form.registeredAddress" placeholder="请输入注册地址" />
        </el-form-item>
        <el-form-item label="注册电话" prop="registeredPhone">
          <el-input v-model="form.registeredPhone" placeholder="请输入注册电话" />
        </el-form-item>
        <el-form-item label="开户银行" prop="bankName">
          <el-input v-model="form.bankName" placeholder="请输入开户银行" />
        </el-form-item>
        <el-form-item label="银行账号" prop="bankAccount">
          <el-input v-model="form.bankAccount" placeholder="请输入银行账号" />
        </el-form-item>
        <el-form-item label="收件人姓名" prop="recipientName">
          <el-input v-model="form.recipientName" placeholder="请输入收件人姓名" />
        </el-form-item>
        <el-form-item label="收件人电话" prop="recipientPhone">
          <el-input v-model="form.recipientPhone" placeholder="请输入收件人电话" />
        </el-form-item>
        <el-form-item label="收件地址" prop="recipientAddress">
          <el-input v-model="form.recipientAddress" placeholder="请输入收件地址" />
        </el-form-item>
        <el-form-item label="行程单接收邮箱" prop="recipientEmail">
          <el-input v-model="form.recipientEmail" placeholder="请输入行程单接收邮箱" />
        </el-form-item>
        <el-form-item label="快递公司" prop="expressCompany">
          <el-select v-model="form.expressCompany" placeholder="请选择快递公司">
            <el-option
              v-for="dict in dict.type.t_experss_company"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="快递单号" prop="trackingNumber">
          <el-input v-model="form.trackingNumber" placeholder="请输入快递单号" />
        </el-form-item>
        <el-form-item label="行程单接收邮箱" prop="shippingTime">
          <el-date-picker clearable
            v-model="form.shippingTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择行程单接收邮箱">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="申请日期" prop="applicationDate">
          <el-date-picker clearable
            v-model="form.applicationDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择申请日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="行程开始日期" prop="travelStartDate">
          <el-date-picker clearable
            v-model="form.travelStartDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择行程开始日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="行程结束日期" prop="travelEndDate">
          <el-date-picker clearable
            v-model="form.travelEndDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择行程结束日期">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="行程人手机号" prop="travelerPhone">
          <el-input v-model="form.travelerPhone" placeholder="请输入行程人手机号" />
        </el-form-item>
        <el-form-item label="行程数量" prop="travelCount">
          <el-input v-model="form.travelCount" placeholder="请输入行程数量" />
        </el-form-item>
        <el-form-item label="行程合计金额" prop="travelAmount">
          <el-input v-model="form.travelAmount" placeholder="请输入行程合计金额" />
        </el-form-item>
        <el-form-item label="创建人" prop="createdBy">
          <el-input v-model="form.createdBy" placeholder="请输入创建人" />
        </el-form-item>
        <el-form-item label="创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listManagementOne, getManagementOne, delManagementOne, addManagementOne, updateManagementOne } from "@/api/boss/managementOne"

export default {
  name: "ManagementOne",
  dicts: ['t_invoice_type', 't_experss_company', 't_invoice_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 发票详情表格数据
      managementOneList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        userId: [
          { required: true, message: "关联用户ID不能为空", trigger: "blur" }
        ],
        invoiceTitle: [
          { required: true, message: "发票抬头不能为空", trigger: "blur" }
        ],
        taxIdentifier: [
          { required: true, message: "纳税人识别号不能为空", trigger: "blur" }
        ],
        invoiceAmount: [
          { required: true, message: "发票金额不能为空", trigger: "blur" }
        ],
        recipientName: [
          { required: true, message: "收件人姓名不能为空", trigger: "blur" }
        ],
        recipientPhone: [
          { required: true, message: "收件人电话不能为空", trigger: "blur" }
        ],
        recipientAddress: [
          { required: true, message: "收件地址不能为空", trigger: "blur" }
        ],
        applicationDate: [
          { required: true, message: "申请日期不能为空", trigger: "blur" }
        ],
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询发票详情列表 */
    getList() {
      this.loading = true
      listManagementOne(this.queryParams).then(response => {
        this.managementOneList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        invoiceId: null,
        userId: null,
        invoiceStatus: null,
        rejectReason: null,
        invoiceTitle: null,
        taxIdentifier: null,
        invoiceType: null,
        invoiceNumber: null,
        invoiceSubject: null,
        invoiceContent: null,
        invoiceAmount: null,
        registeredAddress: null,
        registeredPhone: null,
        bankName: null,
        bankAccount: null,
        recipientName: null,
        recipientPhone: null,
        recipientAddress: null,
        recipientEmail: null,
        expressCompany: null,
        trackingNumber: null,
        shippingTime: null,
        applicationDate: null,
        travelStartDate: null,
        travelEndDate: null,
        travelerPhone: null,
        travelCount: null,
        travelAmount: null,
        createdBy: null,
        createdAt: null,
        updatedAt: null
      }
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.invoiceId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加发票详情"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const invoiceId = row.invoiceId || this.ids
      getManagementOne(invoiceId).then(response => {
        this.form = response.data
        this.open = true
        this.title = "修改发票详情"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.invoiceId != null) {
            updateManagementOne(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addManagementOne(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const invoiceIds = row.invoiceId || this.ids
      this.$modal.confirm('是否确认删除发票详情编号为"' + invoiceIds + '"的数据项？').then(function() {
        return delManagementOne(invoiceIds)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('boss/managementOne/export', {
        ...this.queryParams
      }, `managementOne_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
